package cn.edu.xjtu.nbrash.xbarsuite.terry;

import com.google.zxing.client.android.Contents;
import com.google.zxing.client.android.Intents;

import cn.edu.xjtu.nbrash.xbarsuite.R;
import cn.edu.xjtu.nbrash.xbarsuite.R.id;
import cn.edu.xjtu.nbrash.xbarsuite.R.layout;
import cn.edu.xjtu.nbrash.xbarsuite.R.menu;
import cn.edu.xjtu.nbrash.xbarsuite.zxingdemo.RashDisplayActivity;
import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.text.InputFilter;
import android.text.InputType;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.os.Build;
import android.provider.ContactsContract;

public class EncodeContact extends Activity {

	private static final int PICK_CONTACT = 0;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_encode_contact);
		final EditText nameText = (EditText) findViewById(R.id.name_text);

		final EditText phoneEdit = (EditText) findViewById(R.id.phone_text);
		phoneEdit.setInputType(InputType.TYPE_CLASS_PHONE);
		phoneEdit.setFilters(new InputFilter[] { new InputFilter.LengthFilter(
				11) });
	}

	public void showQR(View v) {
		String name = ((TextView) findViewById(R.id.name_text)).getText()
				.toString();
		String phone = ((TextView) findViewById(R.id.phone_text)).getText()
				.toString();
		// String[] phoneArray = {phone};
		String email = ((TextView) findViewById(R.id.email_text)).getText()
				.toString();
		// String[] emailArray = {email};
		String company = ((TextView) findViewById(R.id.company_text)).getText()
				.toString();
		String postal = ((TextView) findViewById(R.id.postal_text)).getText()
				.toString();
		String url = ((TextView) findViewById(R.id.url_text)).getText()
				.toString();
		String note = ((TextView) findViewById(R.id.note_text)).getText()
				.toString();
		final EditText emailEdit = (EditText) findViewById(R.id.email_text);
		final EditText postalEdit = (EditText) findViewById(R.id.postal_text);


		
		
		if(phone != null && phone.length() != 0&&(phone.length()<11||phone.charAt(0)!='1')){
				//提示用户输入的字符长度不够，可以用TOAST也可以用DIALOG
				
				Toast.makeText(this, "请输入正确的手机号码！", Toast.LENGTH_LONG).show();
				}
		
	
		else if(email != null && email.length() != 0&&!isEmail(emailEdit.getText().toString())){
				
				
				Toast.makeText(this, "请输入正确的email地址！", Toast.LENGTH_LONG).show();
		}
		
		else{
			// if(text != null && !text.equals("")){
			Intent intent = new Intent(this, RashDisplayActivity.class);
			Bundle bundle = new Bundle();
			// ACTION 是 ENCODE
			intent.setAction(Intents.Encode.ACTION);
			// 类型是TEXT，这里可以有很多中类型，具体参见QRCodeEncoder源码
			bundle.putString(ContactsContract.Intents.Insert.NAME, name);
			bundle.putString(ContactsContract.Intents.Insert.COMPANY, company);
			bundle.putString(ContactsContract.Intents.Insert.POSTAL, postal);
			bundle.putString(Contents.URL_KEY, url);
			bundle.putString(ContactsContract.Intents.Insert.EMAIL, email);
			bundle.putString(ContactsContract.Intents.Insert.PHONE, phone);
			bundle.putString(Contents.NOTE_KEY, note);

			intent.putExtra(Intents.Encode.TYPE, Contents.Type.CONTACT);
			// 数据，这里是字符串
			intent.putExtra(Intents.Encode.DATA, bundle);
			startActivity(intent);
			// }
			
		}
	}

	public void back(View v) {
		finish();
	}

	public void pickFromContact(View v) {

		Intent intent = new Intent(Intent.ACTION_PICK,
				ContactsContract.Contacts.CONTENT_URI);
		startActivityForResult(intent, PICK_CONTACT);

	}

	@Override
	public void onActivityResult(int reqCode, int resultCode, Intent data) {
		super.onActivityResult(reqCode, resultCode, data);

		switch (reqCode) {
		case (PICK_CONTACT):
			if (resultCode == Activity.RESULT_OK) {
				Uri contactData = data.getData();
				Cursor c1 = managedQuery(contactData, null, null, null, null);

				if (c1.moveToFirst()) {
					String contactName = c1
							.getString(c1
									.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));

					TextView nameText = (TextView) findViewById(R.id.name_text);
					nameText.setText(contactName);
					// 提取电话号
					String contactId = c1.getString(c1
							.getColumnIndex(ContactsContract.Contacts._ID));
					Cursor phone = getContentResolver().query(
							ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
							null,
							ContactsContract.CommonDataKinds.Phone.CONTACT_ID
									+ " = " + contactId, null, null);

					String number = "";
					while (phone.moveToNext()) {
						String strPhoneNumber = phone
								.getString(phone
										.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

						number = strPhoneNumber ;
					}

					TextView phoneText = (TextView) findViewById(R.id.phone_text);
					phoneText.setText(number);
					// 提取邮箱
					Cursor email = getContentResolver().query(
							ContactsContract.CommonDataKinds.Email.CONTENT_URI,
							null,
							ContactsContract.CommonDataKinds.Email.CONTACT_ID
									+ " = " + contactId, null, null);

					String emailStr = "";
					while (email.moveToNext()) {
						String strEmailNumber = email
								.getString(email
										.getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));

						emailStr = strEmailNumber;
					}

					TextView emailText = (TextView) findViewById(R.id.email_text);
					emailText.setText(emailStr);
					// 提取地址
					Cursor add = getContentResolver()
							.query(ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
									null,
									ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID
											+ " = " + contactId, null, null);

					String addStr = "";
					while (add.moveToNext()) {
						String strAddNumber = add
								.getString(add
										.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.DATA));

						addStr = strAddNumber;
					}

					TextView addText = (TextView) findViewById(R.id.address_text);
					addText.setText(addStr);
					// 提取公司

				}
			}
			break;
		}
	}
		public boolean isEmail(String str){
			
	
			if(str.matches("^\\w+@\\w+\\.(com|cn)")) return true;
	//		if(str.matches("\\w+(\\.\\w+)*@\\w+(\\.\\w+)+")) return true;
					   
			return false;
		}
		  public boolean isUrl(String url){  
		        if(url.matches("^http://[a-zA-Z0-9./\\s]+.shtml")){  
		            return true;  
		        }else{  
		            return false;  
		        }  
		    }
}
